﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Newtonsoft.Json;
using FaWorksSolution.MvcApplication.BusinessLogicLayer;
using FaWorksSolution.MvcApplication.Models;
using FaWorksSolution.MvcApplication.Models.Account;

namespace FaWorksSolution.MvcApplication.Controllers
{
    public class UserController : Controller
    {
        /// <summary>
        /// 退出
        /// </summary>
        /// <returns></returns>
        public ActionResult Logout()
        {
            AjaxResponse ar = new AjaxResponse
            {
                IsSuccess = true
            };
            FormsAuthentication.SignOut();
            return Json(ar, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [ActionName("Login"), HttpGet]
        public ActionResult Logon(string returnUrl)
        {
            if (!string.IsNullOrEmpty(returnUrl) && 
                returnUrl.StartsWith("/Manage", StringComparison.OrdinalIgnoreCase)) {
                return View("~/Views/Manage/Login.cshtml");
            }

            return View();
        }

        [ActionName("Login"), HttpPost]
        public ActionResult Logon(string userName, string userPswd, bool isRemember)
        {
            try
            {
                var user = urDb.AccountUsers.FirstOrDefault(m => m.UserName == userName);
                if (user == null)
                {
                    throw new KeyNotFoundException("该用户不存在！");
                }
                if (user.Password != userPswd) {
                    throw new Exception("密码不正确！");
                }

                int expiration = 0;
                if (isRemember)
                {
                    expiration = 365 * 24 * 60; //一年
                }
                FormsPrincipal<User>.SignIn(user, expiration, this.HttpContext);

                return Redirect("/manage");
            }
            catch (Exception ex) {
                AjaxResponse ar = new AjaxResponse();
                ar.IsSuccess = false;
                ar.Data = ex.Message;
                return Json(ar);
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        [ActionName("Register"), HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [ActionName("Register"), HttpPost]
        public ActionResult Login(User user)
        {
            user.UserId = Guid.NewGuid();
            urDb.AccountUsers.Add(user);
            urDb.SaveChanges();

            return View("Login");
        }

        /// <summary>
        /// 忘记密码
        /// </summary>
        /// <returns></returns>
        public ActionResult Forgot()
        {
            return View();
        }

        private DefaultContext urDb = new DefaultContext();
    }
}
